package com.lagou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lagou.mapper.LagouAuthCodeMapper;
import com.lagou.mapper.LagouTokenMapper;
import com.lagou.model.LagouAuthCode;
import com.lagou.model.LagouToken;
import com.lagou.service.ILagouAuthCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohuzi
 * @since 2022-03-26
 */
@Service
public class LagouAuthCodeServiceImpl extends ServiceImpl<LagouAuthCodeMapper, LagouAuthCode> implements ILagouAuthCodeService {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private LagouAuthCodeMapper lagouAuthCodeMapper;

    @Autowired
    private LagouTokenMapper lagouTokenMapper;

    @Value("${code.expirelength}")
    private String expireLength;

    @Override
    public void addUserCode(String code, String email) {
        Date date = new Date();
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setCode(code);
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCreatetime(date);
        lagouAuthCode.setExpiretime(new Date(date.getTime() + Long.parseLong(expireLength)));
        lagouAuthCodeMapper.insert(lagouAuthCode);
    }

    @Override
    public int addUserToken(String token, String email) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(token);
        return lagouTokenMapper.insert(lagouToken);
    }

    @Override
    public Integer validateCode(String email, String code) {
        QueryWrapper<LagouAuthCode> queryWrapper = new QueryWrapper<>();
        QueryWrapper<LagouAuthCode> eq = queryWrapper.eq("code", code)
                .orderByDesc("createtime");
        List<LagouAuthCode> lagouAuthCodes = lagouAuthCodeMapper.selectList(eq);
        if (CollectionUtils.isEmpty(lagouAuthCodes)) {
            throw new RuntimeException("code not exists");
        }
        LagouAuthCode lagouAuthCode = lagouAuthCodes.get(0);
        if (lagouAuthCode.getExpiretime().getTime() < new Date().getTime()) {

            // 超时

            return 2;
        } else if (!code.equals(lagouAuthCode.getCode())) {

            return 1;
        } else {

            return 0;
        }
    }

    @Override
    public String getEmailByToken(String token) {
        QueryWrapper<LagouToken> queryWrapper=new QueryWrapper<LagouToken>().select("distinct email,token  ").eq("token",token);
        LagouToken lagouToken = lagouTokenMapper.selectOne(queryWrapper);
        if(lagouToken!=null){
            return lagouToken.getEmail();
        }
        return "";
    }
}
